package com.ruoyi.web.controller.api.suggest;

import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.bo.PMessageBo;
import com.ruoyi.system.domain.vo.PMessageVo;
import com.ruoyi.system.service.IPMessageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * @Description:
 * @Author: wk
 * @Date: 2022/7/29 16:29
 * @Version: 1.0
 */
@Api(value = "消息控制器", tags = {"前端-消息管理"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/font/message")
public class MessageApiController {
    private final IPMessageService iPMessageService;

    /**
     * 查询消息列表
     */
    @ApiOperation("查询消息列表")
    @GetMapping("/list")
    public TableDataInfo<PMessageVo> list(PMessageBo bo, PageQuery pageQuery) {
        return iPMessageService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出消息列表
     */
    @ApiOperation("导出消息列表")
    @PostMapping("/export")
    public void export(PMessageBo bo, HttpServletResponse response) {
        List<PMessageVo> list = iPMessageService.queryList(bo);
        ExcelUtil.exportExcel(list, "消息", PMessageVo.class, response);
    }

    /**
     * 获取消息详细信息
     */
    @ApiOperation("获取消息详细信息")
    @GetMapping("/{id}")
    public R<PMessageVo> getInfo(@ApiParam("主键")
                                 @NotNull(message = "主键不能为空")
                                 @PathVariable("id") Long id) {
        return R.ok(iPMessageService.queryById(id));
    }

    /**
     * 新增消息
     */
    @ApiOperation("新增消息")
    @RepeatSubmit()
    @PostMapping("/add")
    public R<Void> add(@Validated(AddGroup.class) @RequestBody PMessageBo bo) {
        iPMessageService.insertByBo(bo);
        return R.ok();
    }

    /**
     * 修改消息
     */
    @ApiOperation("修改消息")
    @RepeatSubmit()
    @PostMapping("/edit")
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody PMessageBo bo) {
        iPMessageService.updateByBo(bo);
        return R.ok();
    }

    /**
     * 删除消息
     */
    @ApiOperation("删除消息")
    @PostMapping("/del/{ids}")
    public R<Void> remove(@ApiParam("主键串")
                          @NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        iPMessageService.deleteWithValidByIds(Arrays.asList(ids), true);
        return R.fail();
    }
}
